package com.hnevc.zz.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hnevc.zz.R;
import com.hnevc.zz.database.RechargeDatabase;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class RechargeActivity extends AppCompatActivity {
    private EditText et_phone,et_money;
    private Button btn_ok,btn_cancel;
    String phone,money;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        et_phone=findViewById(R.id.et_phone_chrge);
        btn_ok=findViewById(R.id.btn_ok);
        btn_cancel=findViewById(R.id.btn_cancel);
        et_money=findViewById(R.id.et_money);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String date = format.format(calendar.getTime());
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RechargeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone=et_phone.getText().toString();
                money=et_money.getText().toString();
                if (phone!=null&&money!=null){
                    RechargeDatabase database=RechargeDatabase.getInstance(RechargeActivity.this);
                    SQLiteDatabase db = database.getWritableDatabase();
                    ContentValues contentValues=new ContentValues();
                    contentValues.put("money",money);
                    contentValues.put("date",date);
                    contentValues.put("phone",phone);
                    db.insert("record",null,contentValues);
                    db.close();
                    database.close();
                    Toast.makeText(RechargeActivity.this,"充值成功",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(RechargeActivity.this,MainActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(RechargeActivity.this,"信息输入有误",Toast.LENGTH_SHORT).show();
                }
            }
        });





    }
}
